<?php
namespace backend\models;

use Yii;
use yii\base\Exception;
use yii\base\Model;
use common\models\XfbUser;

/**
 * Register form
 */
class RegisterForm extends Model
{
    public $username;
    public $email;
    public $password;
    public $retype_password;
    public $mobile;
    public $register_sms_code;
    public $corp_name;
    public $province;
    public $city;
    public $district;
    public $address;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['username', 'trim'],
            ['username', 'required'],
            ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => '此用户名已被占用.'],
            ['username', 'string', 'min' => 4, 'max' => 255],

            ['password', 'required'],
            ['password', 'string', 'min' => 6],

            ['retype_password', 'required'],
            ['retype_password', 'compare', 'compareAttribute' => 'password'],

            ['mobile', 'required'],
            ['mobile', 'unique', 'targetClass' => '\common\models\User', 'message' => '此手机号已被占用.'],
            ['mobile', 'match', 'pattern' => '/^1[0-9]{10}$/', 'message' => '手机号必须为1开头的11位纯数字'],

            ['register_sms_code', 'required'],
            ['register_sms_code', 'integer'],

            ['corp_name', 'required'],
            ['corp_name', 'string', 'min' => 2, 'max' => 255],

            [['province', 'city', 'district', 'address'], 'required']
        ];
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels()
    {
        return array(
            'username' => '用户名',
            'email' => '邮箱',
            'mobile' => '手机号',
            'password' => '注册密码',
            'retype_password' => '再次输入密码',
            'register_sms_code' => '输入验证码',
        );
    }

    /**
     * register
     *
     * @return User|null the saved model or null if saving fails
     */
    public function register()
    {
        if (!$this->validate()) {
            return null;
        }

        # 开启事务 会员信息与企业信息同步保存
        $transaction = Yii::$app->db->beginTransaction();
        try {
            $time = time();
            $user = new User();
            $user->username = $this->username;
            $user->setPassword($this->password);
            $user->generateAuthKey();
            $user->mobile = $this->mobile;
            $user->created_at = $time;
            $user->updated_at = $time;
            $user->save();

            $transaction->commit();
            return $user;
        } catch (Exception $e) {
            $transaction->rollBack();
            return null;
        }
    }

    /**
     * @return int
     */
    public function getSmsCode()
    {
        //检查session是否打开

        if (!Yii::$app->session->isActive) {
            Yii::$app->session->open();
        }
        //取得验证码和短信发送时间session
        $register_sms_code = intval(Yii::$app->session->get('register_sms_code'));
        $register_sms_time = Yii::$app->session->get('register_sms_time');

        if (time() - $register_sms_time < 600) {
            return $register_sms_code;
        } else {
            return 0000;
        }
    }


}
